//
//  MXLockCircle.m
//  MXLoginMethodsDomo
//
//  Created by 马彦 on 16/9/27.
//  Copyright © 2016年 MX. All rights reserved.
//
/**
 圆圈
 */

#import "MXLockCircle.h"
#import "MXLockConfig.h"

@implementation MXLockCircle

- (instancetype)initWithDiameter:(CGFloat)diameter {
    self = [super initWithFrame:CGRectMake(0, 0, diameter, diameter)];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.diameter = diameter;
        self.state = MXLockCircleStateNormal;
    }
    return self;
}

- (void)updateCircleState:(MXLockCircleState)state {
    _state = state;
    [self setNeedsDisplay];
}

/**画圆*/
- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, MXLOCK_CIRCLE_WIDTH);
    
    switch (self.state) {
        case MXLockCircleStateNormal:{
            [self drawEmptyCircleWithContext:context rect:CGRectMake(MXLOCK_CIRCLE_WIDTH / 2, MXLOCK_CIRCLE_WIDTH / 2, self.diameter - MXLOCK_CIRCLE_WIDTH, self.diameter - MXLOCK_CIRCLE_WIDTH) strokeColor:MXLOCK_COLOR_NORMAL fillColor:MXLOCK_COLOR_BACKGROUND];
        }
            break;
        case MXLockCircleStateSelected:{
            [self drawCenterCircleWithContext:context rect:CGRectMake(MXLOCK_CIRCLE_WIDTH / 2, MXLOCK_CIRCLE_WIDTH / 2, self.diameter - MXLOCK_CIRCLE_WIDTH, self.diameter - MXLOCK_CIRCLE_WIDTH) centerRect:CGRectMake(self.diameter * (0.5 - MXLOCK_CIRCLE_CENTER_RATIO / 2), self.diameter * (0.5 - MXLOCK_CIRCLE_CENTER_RATIO / 2), self.diameter * MXLOCK_CIRCLE_CENTER_RATIO, self.diameter * MXLOCK_CIRCLE_CENTER_RATIO) strokeColor:MXLOCK_COLOR_NORMAL fillColor:MXLOCK_COLOR_BACKGROUND];
        }
            break;
        case MXLockCircleStateFill:{
            [self drawSolidCircleWithContext:context rect:CGRectMake(MXLOCK_CIRCLE_WIDTH / 2, MXLOCK_CIRCLE_WIDTH / 2, self.diameter - MXLOCK_CIRCLE_WIDTH, self.diameter - MXLOCK_CIRCLE_WIDTH) strokeColor:MXLOCK_COLOR_NORMAL];
        }
            break;
        case MXLockCircleStateError:{
            [self drawCenterCircleWithContext:context rect:CGRectMake(MXLOCK_CIRCLE_WIDTH / 2, MXLOCK_CIRCLE_WIDTH / 2, self.diameter - MXLOCK_CIRCLE_WIDTH, self.diameter - MXLOCK_CIRCLE_WIDTH) centerRect:CGRectMake(self.diameter * (0.5 - MXLOCK_CIRCLE_CENTER_RATIO / 2), self.diameter * (0.5 - MXLOCK_CIRCLE_CENTER_RATIO / 2) , self.diameter * MXLOCK_CIRCLE_CENTER_RATIO, self.diameter * MXLOCK_CIRCLE_CENTER_RATIO) strokeColor:MXLOCK_COLOR_ERROR fillColor:MXLOCK_COLOR_BACKGROUND];
        }
            break;
        
        default:
            break;
    }
}


/**
 空心圆环

 @param context     图形上下文
 @param rect        位置和大小
 @param strokeColor 图形上下文颜色
 @param fillColor   填充颜色
 */
- (void)drawEmptyCircleWithContext:(CGContextRef)context rect:(CGRect)rect strokeColor:(UIColor *)strokeColor fillColor:(UIColor *)fillColor {
    CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
    CGContextSetFillColorWithColor(context, fillColor.CGColor);
    CGContextAddEllipseInRect(context, rect);
    CGContextDrawPath(context, kCGPathFillStroke);
}


/**
 实心圆

 @param context     图形上下文
 @param rect        位置和大小
 @param strokeColor 颜色
 */
- (void)drawSolidCircleWithContext:(CGContextRef)context rect:(CGRect)rect strokeColor:(UIColor *)strokeColor {
    CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
    CGContextSetFillColorWithColor(context, strokeColor.CGColor);
    CGContextAddEllipseInRect(context, rect);
    CGContextDrawPath(context, kCGPathFillStroke);
}


/**
 圆环 + 中心小圆

 @param context     图形上下文
 @param rect        大圆位置和大小
 @param centerRect  小圆位置和大小
 @param strokeColor 颜色
 @param fillColor   小圆填充颜色
 */
- (void)drawCenterCircleWithContext:(CGContextRef)context rect:(CGRect)rect centerRect:(CGRect)centerRect strokeColor:(UIColor *)strokeColor fillColor:(UIColor *)fillColor {
    
    CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
    CGContextAddEllipseInRect(context, rect);
    CGContextDrawPath(context, kCGPathStroke);
    
    CGContextSetFillColorWithColor(context, strokeColor.CGColor);
    CGContextAddEllipseInRect(context, centerRect);
    CGContextDrawPath(context, kCGPathFillStroke);
 
}

@end
